<template>
  <!-- 问题澄清表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="700px"
    draggable
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      style="margin-top: -20px"
    >
      <el-row>
        <el-col :span="24"> <h4>是否同意</h4> </el-col>
        <el-col :span="24">
          <el-form-item prop="agreeWith">
            <div>
              <el-radio-group
                v-model="formData.agreeWith"
                v-if="!props.isReadonly"
                @change="changAgreeWith"
              >
                <el-radio :label="1">1.同意</el-radio>
                <el-radio :label="0">2.澄清</el-radio>
              </el-radio-group>
              <span v-else>{{ formData.agreeWith }}</span>
            </div>
          </el-form-item>
        </el-col>
        <el-col
          :span="24"
          style="margin-top: -20px"
          v-if="formData.agreeWith == 0"
        >
          <h4>澄清理由</h4>
        </el-col>
        <el-col :span="24" v-if="formData.agreeWith == 0">
          <el-form-item prop="reasonCode">
            <el-radio-group
              v-model="formData.reasonCode"
              v-if="!props.isReadonly"
              @change="changAgreeWith"
            >
              <el-radio :label="0">1.非我们的产品</el-radio>
              <el-radio :label="1">2.非我们过程形成的缺陷</el-radio>
              <el-radio :label="2">3.其他</el-radio>
            </el-radio-group>

            <span v-else>{{ formData.reasonCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="formData.agreeWith == 0">
          <el-form-item>
            <el-input
              :autosize="{ minRows: 6, maxRows: 6 }"
              v-if="!props.isReadonly"
              v-model="formData.reasonContent"
              maxlength="255"
              style="width: 850px"
              placeholder="请输入澄清理由内容"
              show-word-limit
              type="textarea"
            />
            <span v-else>{{ formData.reasonContent }}</span>
          </el-form-item>
        </el-col>
        <div
          style="
            margin-top: 20px;
            width: 100%;
            max-height: 200px;
            overflow-y: auto;
          "
          id="table"
        >
          <CommonUploadFile
            :isRead="props.isReadonly"
            :files="formData.assetPhotos"
            @update-files="updateFormFiles"
          />
        </div>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "QMSQ8DISSUECLARIFICATIONADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  qualityExceptionId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  qualityExceptionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  agreeWith: [],
  reasonCode: [],
  reasonContent: [
    { required: true, message: "请填写澄清理由", trigger: "blur" },
    { min: 1, max: 1000, message: "长度在 1 到 1000 个字符", trigger: "blur" },
  ],
  evaluationResult: [],
  evaluationResultUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  evaluationResultUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  evaluationResultContent: [
    { min: 1, max: 1000, message: "长度在 1 到 1000 个字符", trigger: "blur" },
  ],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  agreeWith: 1,
  reasonCode: 0,
  reasonContent: "",
  evaluationResult: "",
  evaluationResultUserId: "",
  evaluationResultUserName: "",
  evaluationResultContent: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  assetPhotos: [],
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        supplyId,
        supplyCode,
        supplyName,
        agreeWith,
        reasonCode,
        reasonContent,
        evaluationResult,
        evaluationResultUserId,
        evaluationResultUserName,
        evaluationResultContent,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        assetPhotos,
      } = newVal;
      Object.assign(formData, {
        qualityExceptionId,
        qualityExceptionCode,
        supplyId,
        supplyCode,
        supplyName,
        agreeWith: agreeWith ? agreeWith : 1,
        reasonCode: reasonCode ? reasonCode : 0,
        reasonContent,
        evaluationResult,
        evaluationResultUserId,
        evaluationResultUserName,
        evaluationResultContent,
      });
      formData.qualityExceptionId = id;
      formData.qualityExceptionCode = qualityProblemCode;
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.qualityExceptionId = "";
  formData.qualityExceptionCode = "";
  formData.supplyId = "";
  formData.supplyCode = "";
  formData.supplyName = "";
  formData.agreeWith = 1;
  formData.reasonCode = 0;
  formData.reasonContent = "";
  formData.evaluationResult = "";
  formData.evaluationResultUserId = "";
  formData.evaluationResultUserName = "";
  formData.evaluationResultContent = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.assetPhotos = [];
}

const loading = ref(false);

/** 问题澄清保存提交 */
function handleSubmit() {
  console.log(formData);
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      const text = formData.id ? "问题确认！" : "问题确认！";
      console.log(formData.agreeWith, formData.reasonContent);

      if (
        formData.agreeWith == 0 &&
        (formData.reasonContent == "" || formData.reasonContent == undefined)
      ) {
        ElMessage.error("选择澄清时请填写澄清理由");
        return;
      }
      loading.value = true;
      await request({
        url: "/qms/q8dissueclarification/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

const changAgreeWith = () => {
  console.log(formData.agreeWith);
};
// 图片处理
function updateFormFiles(val: []) {
  formData.assetPhotos = val;
}
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
